﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;

namespace DPortAck.ObjectModel
{
    public class ConnectionTest
    {
        public static int TIMEOUT_DEFAULT = 5000;

        private int _timeout = TIMEOUT_DEFAULT;
        private List<MachineConnectionTest> _machineTests = null;
        private string _name;

        public String Name
        {
            get { return _name; }
            set { _name = value; }
        }
        
        public int Timeout 
        {
            get { return _timeout; }
            set { _timeout = value; }
        }

        public void AddMachineConnectionTest(MachineConnectionTest machineTest)
        {
            if (machineTest == null)
                throw new ArgumentNullException("machineTest");

            if (_machineTests == null)
                _machineTests = new List<MachineConnectionTest>();

            machineTest.Test = this;
            _machineTests.Add(machineTest);
        }

        public ConnectionTestResult Execute()
        {
            ConnectionTestResult result = new ConnectionTestResult(this);
            result.Succeded = true;

            if (_machineTests != null && _machineTests.Count > 0)
            {
                foreach (MachineConnectionTest machineTest in _machineTests)
                {
                    MachineConnectionTestResult machineTestResult = machineTest.Execute();

                    if (!machineTestResult.Succeded)
                    {
                        result.Succeded = false;
                    }

                    result.AddTestResult(machineTestResult);
                }
            }

            return result;
        }

        public override string ToString()
        {
            return string.Format("{0} (MaxTimeout: {1})", _name, _timeout);
        }
    }
}
